package com.Tools.Calligraphy
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	public class Calligraphy extends Sprite
	{
		private var content:Sprite;//用来容纳显示的容器
		
		private var board:Sprite;//用来探测轨迹的黑板
		
		private var board_width:Number;
		
		private var board_height:Number;
		
		private var timer:Timer=new Timer(50);//点记录周期
		
		private var last_point:Point;//上一次记录点
		
		public function Calligraphy(_width:Number,_height:Number)
		{
			super();
			
			board_width=_width;
			board_height=_height;
			
			content=new Sprite();
			board=new Sprite();
			this.addChild(content);
			this.addChild(board);
			
			//初始化黑板
			board.graphics.beginFill(0x000000,0.01);
			board.graphics.drawRect(0,0,board_width,board_height);
			board.graphics.endFill();
			
			board.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			timer.addEventListener(TimerEvent.TIMER,onTimeToRecord);
		}
		
		//当鼠标按下
		private function onMouseDown(event:MouseEvent):void{
			board.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			last_point=new Point(event.localX,event.localY);
			
			var temp_begin:BeginWrite=new BeginWrite(last_point);
			content.addChild(temp_begin);
			
			timer.reset();
			timer.start();
		}
		
		//记录并生成新的点
		private function onTimeToRecord(event:TimerEvent):void{
			var new_point:Point=new Point(this.mouseX,this.mouseY);
			
			if(new_point.x==last_point.x && new_point.y==last_point.y){
				return;
			}
			
			var temp_center:CenterWrite=new CenterWrite(last_point,new_point);
			content.addChild(temp_center);
			
			last_point=new Point(new_point.x,new_point.y);
		}
		
		//鼠标抬起
		private function onMouseUp(event:MouseEvent):void{
			var new_point:Point=new Point(this.mouseX,this.mouseY);
			board.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			timer.stop();
			
			var temp_end:EndWrite=new EndWrite(last_point,new_point);
			content.addChild(temp_end);
			
		}
	}
}